package com.gan.simple_init.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gan.simple_init.common.base.ResponseErrorEnum;
import com.gan.simple_init.common.constant.CommonConstant;
import com.gan.simple_init.common.exception.BusinessException;
import com.gan.simple_init.mapper.SysOperationLogMapper;
import com.gan.simple_init.model.sys.dto.SysOperationLogDTO;
import com.gan.simple_init.model.sys.entity.SysOperationLog;
import com.gan.simple_init.service.SysOperationLogService;
import com.gan.simple_init.util.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author gbw
 * @description 针对表【sys_operation_log(操作日志记录)】的数据库操作Service实现
 * @createDate 2023-08-18 11:05:02
 */
@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLog>
        implements SysOperationLogService {


    /**
     * 根据条件查询操作日志
     *
     * @param sysOperationLogDTO
     * @return
     */
    @Override
    public Page<SysOperationLog> listOperLogByPage(SysOperationLogDTO sysOperationLogDTO) {
        Long pageSize = sysOperationLogDTO.getPageSize();
        Long pageNum = sysOperationLogDTO.getPageNum();

        // 限制爬虫
        if (pageSize > 20) {
            throw new BusinessException(ResponseErrorEnum.ILLEGAL_PARAMS);
        }

        Page<SysOperationLog> operLogPage = this.page(new Page<>(pageNum, pageSize),
                this.getQueryWrapper(sysOperationLogDTO));

        return operLogPage;
    }

    @Override
    public QueryWrapper<SysOperationLog> getQueryWrapper(SysOperationLogDTO sysOperationLogDTO) {

        String sortField = sysOperationLogDTO.getSortField();
        String sortOrder = sysOperationLogDTO.getSortOrder();

        Integer errorFlag = sysOperationLogDTO.getErrorFlag();
        String title = sysOperationLogDTO.getTitle();
        Integer businessType = sysOperationLogDTO.getBusinessType();
        String method = sysOperationLogDTO.getMethod();
        String requestMethod = sysOperationLogDTO.getRequestMethod();
        String operName = sysOperationLogDTO.getOperName();
        String operRole = sysOperationLogDTO.getOperRole();
        String operUrl = sysOperationLogDTO.getOperUrl();
        String operIp = sysOperationLogDTO.getOperIp();
        String operLocation = sysOperationLogDTO.getOperLocation();
        String operParam = sysOperationLogDTO.getOperParam();
        String jsonResult = sysOperationLogDTO.getJsonResult();
        Integer status = sysOperationLogDTO.getStatus();
        String errorMsg = sysOperationLogDTO.getErrorMsg();
        Date operTime = sysOperationLogDTO.getOperTime();
        Long costTime = sysOperationLogDTO.getCostTime();


        QueryWrapper<SysOperationLog> qw = new QueryWrapper<>();
        // 是否查询报错的日志
        qw.ne(errorFlag == 1, "errorMsg", "");
        qw.eq(errorFlag == 2, "errorMsg", "");
        qw.like(StringUtils.isNotEmpty(title), "title", title)
                .like(StringUtils.isNotEmpty(method), "method", method)
                .like(StringUtils.isNotEmpty(requestMethod), "requestMethod", requestMethod)
                .like(StringUtils.isNotEmpty(operName), "operName", operName)
                .like(StringUtils.isNotEmpty(operRole), "operRole", operRole)
                .like(StringUtils.isNotEmpty(operUrl), "operUrl", operUrl)
                .like(StringUtils.isNotEmpty(operLocation), "operLocation", operLocation)
                .like(StringUtils.isNotEmpty(operParam), "operParam", operParam)
                .like(StringUtils.isNotEmpty(jsonResult), "jsonResult", jsonResult)
                .like(StringUtils.isNotEmpty(errorMsg), "errorMsg", errorMsg);
        qw.eq(businessType != null && businessType != 0, "businessType", businessType)
                .eq(operIp != null && operIp != "", "operIp", operIp)
                .eq(status != null && status != 0, "status", status);
        qw.gt(costTime != null && costTime != 0, "costTime", costTime)
                .gt(ObjectUtils.isNotEmpty(operTime), "operTime", operTime);
        qw.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SortOrder.ASC), sortField);

        return qw;
    }
}




